package hot100;

import comment.TreeNode;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: wzp
 * @CreateTime: 2024-09-16  16:50
 * @Description: 验证一棵树是否是二叉搜索树
 * @Version: 1.0
 */
public class hot_98 {
    public boolean isValidBST(TreeNode root) {
        List<Integer> result=new ArrayList<>();
        dfs(result,root);
        boolean tf=true;
        for (int i = 0; i < result.size()-1; i++) {
            if(result.get(i)>result.get(i+1)){
                tf=false;
                break;
            }
        }
        return tf;

    }
//    这道题的解题思路就是利用二叉树的中序遍历，查看中序遍历的结果是不是单调递增的
    public void dfs(List<Integer> result,TreeNode treeNode){
        if(treeNode==null){
            return;
        }
        dfs(result,treeNode.left);
        result.add(treeNode.val);
        dfs(result,treeNode.right);
    }
}
